package com.marvels.financial.loan.service.impl;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.common.utils.CommonProperties;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dao.McfeLendingProcessDao;
import com.marvels.financial.loan.dto.McfeLendingProcess;
import com.marvels.financial.loan.service.LoanPaymentApi;
import com.marvels.financial.loan.service.PaymentResultQueryTaskletApi;

@Service
public class PaymentResultQueryTaskletImpl implements PaymentResultQueryTaskletApi {

	@Autowired
	private LoanPaymentApi loanPaymentApi;


//	@Autowired
//	private CommonProperties properties;

	@Autowired
	private McfeLendingProcessDao mcfeLendingProcessMapper;

	/**
	 * 
	* @Title: paymentResultQuery 
	* @Description:放款结果查询         
	* @author 
	* @date 2018年10月17日 上午9:23:27
	 */
	@Override
	public void paymentResultQuery() {
		MarvelsLogUtil.getInstance().info(">>>>>>放款结果查询定时任务开始");
		String onOff = CommonProperties.getTimingPaymentQuery();// 是否开启定时任务
		if (!"on".equalsIgnoreCase(onOff)) {
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>代付结果查询定时处理任务未开启");
			return;
		}
		long threadId = Thread.currentThread().getId();
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>线程号:" + threadId + "代付结果查询定时处理处理开始");
		// 先锁住一批需要查询结果的数据
		String batName = threadId + "-" + UUID.randomUUID().toString().replace("-", "");
		int count = mcfeLendingProcessMapper.updateAuditBatName(batName, String.valueOf(threadId), "F10",null);// 处理中
		if (count < 1) {// 没有需要放款的数据
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>线程号:" + threadId + "代付结果定时查询没有需要处理的数据:");
			return;
		}

		// 查询出当前批次锁住的需要查询结果的数据
		List<McfeLendingProcess> list = mcfeLendingProcessMapper.selectByBatName(batName, String.valueOf(threadId),"F10");
		if (list != null && list.size() > 0) {
			for (McfeLendingProcess mcfeLendingProcess : list) {
				String loanId = mcfeLendingProcess.getLoanId();
				MarvelsLogUtil.getInstance().info(">>>>>>>>>>线程号:" + threadId + ",loanId" + loanId + "代付结果定时查询开始");
				try {
					loanPaymentApi.loanPaymentQuery(loanId);
				} catch (Exception e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>loanId:"+loanId+",放款查询异常:",e);
				}
				MarvelsLogUtil.getInstance().info(">>>>>>>>>>线程号:" + threadId + ",loanId" + loanId + "代付结果定时查询结束");
			}
		}
		return;
	}

}
